﻿using Furion.RemoteRequest.Extensions;
using Newtonsoft.Json;

namespace apitest.Application
{
    public class HrService : ITransient
    {
        string dev_account = App.Configuration["HR:dev_account"];
        string dev_password = App.Configuration["HR:dev_password"];
        string dev_login_id = App.Configuration["HR:dev_login_id"];
        string dev_login_type = App.Configuration["HR:dev_login_type"];
        static string token = "";
        static string user_id = "";
        ISqlSugarClient _db;
        public HrService(ISqlSugarClient db)
        {
            _db = db;
        }

        [HttpGet, QueryParameters]
        public async Task<dynamic> GetToken(bool ReGet = false)
        {
            if (ReGet || string.IsNullOrWhiteSpace(token))
            {
                var url = "https://hr.astatech.cn/open-api/token/create";
                var res = await url.SetBody(new
                {
                    dev_account,
                    dev_password
                }).PostAsStringAsync();
                var data = CheckHttpResult(res);

                var data1 = JsonConvert.DeserializeObject<HrTokenResultDto>(data.ToString());
                token = data1.dev_token;
                user_id = AesUtil.Encode(dev_login_id, token);
                await Console.Out.WriteLineAsync($"token:{token},user_id:{user_id}");
                return token;
            }
            else
            {
                await Console.Out.WriteLineAsync($"token:{token},user_id:{user_id}");
                return token;
            }
        }

        [HttpGet, QueryParameters]
        public async Task<dynamic> GetUserId(string phone)
        {
            if (string.IsNullOrWhiteSpace(phone))
            {
                phone = dev_login_id;
            }
            await GetToken();
            var res = AesUtil.Encode(phone, token);
            await Console.Out.WriteLineAsync($"token:{token},user_id:{res}");
            return res;
        }

        public async Task<dynamic> getAdminId()
        {
            await GetToken();
            var url = $"https://hr.astatech.cn/api/org.do?method==findAdmin&dev_token={token}";
            var res = await url
                 .SetHeaders(new
                 {
                     dev_token = token
                 })
                .GetAsStringAsync();
            return res;
        }

        public async Task<dynamic> CheckToken()
        {
            await GetToken();
            var url = $"https://hr.astatech.cn/open-api/token/check";
            var res = await url
                .SetHeaders(new
                {
                    dev_token = token
                })
                .GetAsStringAsync();
            return res;
        }

        public async Task<dynamic> GetFormData()
        {
            await GetToken();

            var phone = "13438904933";
            var user_id = AesUtil.Encode(phone, token);

            await Console.Out.WriteLineAsync(user_id);
            //var url = $"https://hr.astatech.cn/api/form.do?method=formData&user_id={user_id}&dev_token={token}";
            var url = $"https://hr.astatech.cn/open-api/form/getFormFieldList&user_id={user_id}&dev_login_type=PHONE&id=1716185442914";
            var body = new
            {
                user_id,
                dev_token = token,
                formId = "1716185442914"
            };
            var res = await url
                .SetHeaders(new
                {
                    dev_token = token
                })
                .PostAsStringAsync();
            return res;
        }




        public async Task<dynamic> findCheckDataList(DateTime? date)
        {
            if (!date.HasValue)
            {
                date = DateTime.Now;
            }
            await GetToken();
            var url = "https://hr.astatech.cn/open-api/attendance/findCheckDataList";
            var queries = new
            {
                dev_token = token,
                user_id,
                dev_login_type = "PHONE"
            };
            var body = new
            {
                checkDate = date,
                pageIndex = 1
            };
            var res = await url.SetQueries(queries)
                    .SetBody(body)
                    .PostAsStringAsync();
            var data = CheckHttpResult(res);
            return data;
        }

        public async Task<List<HrDepartmentResultDto>> GetDeprtmentList()
        {
            await GetToken();
            var url = $"https://hr.astatech.cn/open-api/department/findAll?dev_token={token}&user_id={user_id}&dev_login_type=PHONE&";
            var res = await url.GetAsStringAsync();
            var data = CheckHttpResult(res);
            var departList = JsonConvert.DeserializeObject<List<HrDepartmentResultDto>>(data);
            return departList;
        }

        public async Task<List<HrUserResultDto>> GetUserList()
        {
            await GetToken();
            var url = $"https://hr.astatech.cn/open-api/person/findByPager?dev_token={token}&user_id={user_id}&dev_login_type=PHONE&";
            var body = new
            {
                pageIndex = 1,
                pageSize = 1000,
                needTotal = true,
                queryParam = new List<string>()
            };
            var res = await url
                .SetBody(body)
                .PostAsStringAsync();
            var data = CheckHttpResult(res);
            var result = JsonConvert.DeserializeObject<HrListUserResultDto>(data);
            return result.dataList;
        }




        string CheckHttpResult(string res)
        {
            var response = JsonConvert.DeserializeObject<HrResultDto>(res);
            if (response.code == 0)
            {
                return response.data.ToString(); ;
            }
            else
            {
                throw new Exception(response.message);
            }
        }

    }


}
